// DailyQuestionMapper.java
package com.scuec.groupfive.mapper;

import com.scuec.groupfive.entity.QuestionInfo;
import org.apache.ibatis.annotations.*;
import java.util.Map;

@Mapper
public interface DailyQuestionMapper {
    // 获取今日题目（基于日期生成随机种子）
    @Select("SELECT q.*, cq.options as options, cq.answer as choice_answer, cq.analysis as choice_analysis, " +
            "fq.blanks as blanks, fq.answer_template as answer_template, fq.analysis as fillblank_analysis " +
            "FROM questions q " +
            "LEFT JOIN choice_questions cq ON q.question_id = cq.question_id AND q.question_type = 'single_choice' " +
            "LEFT JOIN fill_blank_questions fq ON q.question_id = fq.question_id AND q.question_type = 'fill_in_the_blank' " +
            "WHERE q.question_type IN ('single_choice', 'fill_in_the_blank') " +
            "ORDER BY RAND(DATE_FORMAT(CURDATE(), '%Y%m%d')) LIMIT 1")
    @Results({
            @Result(property = "questionId", column = "question_id"),
            @Result(property = "questionType", column = "question_type"),
            @Result(property = "content", column = "content"),
            @Result(property = "options", column = "options"),
            @Result(property = "choiceAnswer", column = "choice_answer"),
            @Result(property = "choiceAnalysis", column = "choice_analysis"),
            @Result(property = "blanks", column = "blanks"),
            @Result(property = "answerTemplate", column = "answer_template"),
            @Result(property = "fillblankAnalysis", column = "fillblank_analysis")
    })
    QuestionInfo selectTodayQuestion();

    @Insert("INSERT INTO favorites (user_id, question_id, status, created_at, updated_at) " +
            "VALUES (#{userId}, #{questionId}, 1, NOW(), NOW())")
    int insertFavorite(@Param("userId") Long userId,
                       @Param("questionId") Long questionId);

    @Delete("DELETE FROM favorites WHERE user_id = #{userId} AND question_id = #{questionId}")
    int deleteFavorite(@Param("userId") Long userId,
                       @Param("questionId") Long questionId);

    @Select("SELECT COUNT(*) FROM favorites WHERE user_id = #{userId} AND question_id = #{questionId}")
    int isFavorite(@Param("userId") Long userId,
                   @Param("questionId") Long questionId);

    @Select("SELECT answer as choice_answer, analysis as choice_analysis FROM choice_questions WHERE question_id = #{questionId}")
    Map<String, Object> selectChoiceAnswer(@Param("questionId") Long questionId);

    @Select("SELECT blanks, answer_template, analysis as fillblank_analysis FROM fill_blank_questions WHERE question_id = #{questionId}")
    Map<String, Object> selectFillBlankAnswer(@Param("questionId") Long questionId);

    @Insert("INSERT INTO answer_records (user_id, question_id, question_type, answer, is_correct, score, time_spent, answered_at) " +
            "VALUES (#{userId}, #{questionId}, #{questionType}, #{answer}, #{isCorrect}, #{score}, #{timeSpent}, NOW())")
    int insertAnswerRecord(@Param("userId") Long userId,
                           @Param("questionId") Long questionId,
                           @Param("questionType") String questionType,
                           @Param("answer") String answer,
                           @Param("isCorrect") Boolean isCorrect,
                           @Param("score") Double score,
                           @Param("timeSpent") Integer timeSpent);
}